package com.lenovo.manufacture;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class SupplyListActivity extends AppCompatActivity {

    private ImageView priceDown;
    private ImageView numDown;
    private ListView supplyList;
    private final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://hh1.me:5001/Interface/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    private List<SupplyListData.DataBean> data = new ArrayList<>();
    private SupplyAdapter supplyAdapter=new SupplyAdapter();
    private int z=0;
    private int c;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_supply_list);
        initView();
        getSupplyInfo();
    }

    private void getSupplyInfo() {
        Network network = retrofit.create(Network.class);
        Call<SupplyListData> supplyListDataCall = network.supplyList();
        supplyListDataCall.enqueue(new Callback<SupplyListData>() {
            @Override
            public void onResponse(Call<SupplyListData> call, Response<SupplyListData> response) {
                data = response.body().getData();
                supplyList.setAdapter(supplyAdapter);
                event();
            }

            @Override
            public void onFailure(Call<SupplyListData> call, Throwable t) {

            }
        });

    }
    private void event(){
        priceDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (z==0){
                    priceDown.setImageResource(R.drawable.up);
                    Collections.sort(data, new Comparator<SupplyListData.DataBean>() {
                        @Override
                        public int compare(SupplyListData.DataBean dataBean, SupplyListData.DataBean t1) {
                            int r = dataBean.getPrice() - t1.getPrice();
                            return r;
                        }
                    });
                    z=1;
                }else {
                    priceDown.setImageResource(R.drawable.down);
                    Collections.sort(data, new Comparator<SupplyListData.DataBean>() {
                        @Override
                        public int compare(SupplyListData.DataBean dataBean, SupplyListData.DataBean t1) {
                            int x = t1.getPrice()-dataBean.getPrice() ;
                            return x;
                        }
                    });
                    z=0;
                }
                supplyAdapter.notifyDataSetChanged();
            }
        });
        numDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (c==0){
                    numDown.setImageResource(R.drawable.up);
                    Collections.sort(data, new Comparator<SupplyListData.DataBean>() {
                        @Override
                        public int compare(SupplyListData.DataBean dataBean, SupplyListData.DataBean t1) {


                            int r = dataBean.getNum() - t1.getNum();

                            return r;
                        }
                    });
                    c=1;
                }else {
                    numDown.setImageResource(R.drawable.down);
                    Collections.sort(data, new Comparator<SupplyListData.DataBean>() {
                        @Override
                        public int compare(SupplyListData.DataBean dataBean, SupplyListData.DataBean t1) {
                            int x = t1.getNum()-dataBean.getNum() ;
                            return x;
                        }
                    });
                    c=0;
                }
                supplyAdapter.notifyDataSetChanged();
            }
        });
    }

    private class SupplyAdapter extends BaseAdapter {

        private TextView supplyCommodityText;
        private TextView supplyPriceText;
        private TextView supplyNumText;
        private TextView supplySupplierText;

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = LayoutInflater.from(SupplyListActivity.this).inflate(R.layout.supply_item, viewGroup, false);
            supplyCommodityText = (TextView) view.findViewById(R.id.supply_commodity_text);
            supplyPriceText = (TextView) view.findViewById(R.id.supply_price_text);
            supplyNumText = (TextView) view.findViewById(R.id.supply_num_text);
            supplySupplierText = (TextView) view.findViewById(R.id.supply_supplier_text);
            supplyCommodityText.setText(data.get(i).getMaterialName());
            supplyNumText.setText(String.valueOf(data.get(i).getNum()));
            supplyPriceText.setText(String.valueOf(data.get(i).getPrice()));
            supplySupplierText.setText(data.get(i).getSupplyName());

            return view;
        }


    }

    private void initView() {
        priceDown = (ImageView) findViewById(R.id.price_down);
        numDown = (ImageView) findViewById(R.id.num_down);
        supplyList = (ListView) findViewById(R.id.supply_list);
    }
}